const chokidar = require('chokidar')
const bodyParser = require('body-parser')
const chalk = require('chalk')
const path = require('path')
const Mock = require('mockjs')

const mockDir = path.join(process.cwd(), 'mock')

// 模拟路由
function unregisterRoutes() {
  Object.keys(require.cache).forEach(i => {
    if (i.includes(mockDir)) {
      delete require.cache[require.resolve(i)]
    }
  })
}

// 模拟http响应
const responseFake = (url, type, respond) => {
  return {
    url: new RegExp(`${process.env.VUE_APP_BASE_API}${url}`),
    type: type || 'get',
    response(req, res) {
      console.log('request invoke:' + req.path)
      res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond))
    }
  }
}

module.exports = app => {
	app.use(bodyParser.json())
	app.use(bodyParser.urlencoded({
		extended: true
	}))

	const mockRoutes = registerRoutes(app)
	var mockRoutesLength = mockRoutes.mockRoutesLength
	var mockStartIndex = mockRoutes.mockStartIndex

	chokidar.watch(mockDir, {
		ignored: /mock-server/,
		ignoreInitial: true
	}).on('all', (event, path) => {
		if (event === 'change' || event === 'add') {
		try {
			app._router.stack.splice(mockStartIndex, mockRoutesLength)

			unregisterRoutes()

			const mockRoutes = registerRoutes(app)
			mockRoutesLength = mockRoutes.mockRoutesLength
			mockStartIndex = mockRoutes.mockStartIndex

			console.log(chalk.magentaBright(`\n > Mock Server hot reload success! changed  ${path}`))
		} catch (error) {
			console.log(chalk.redBright(error))
		}
		}
	})
}